//
//  ZZInFactoryViewController.m
//  新项目
//
//  Created by liGordon on 17/3/23.
//  Copyright © 2017年 刘耀宗. All rights reserved.
//

#import "ZZInFactoryViewController.h"
#import "ZZInFactoryViewModel.h"
#import "ZZInFactoryCell.h"
#import "ZZInFactoryModel.h"
#import "ZZInFactoryContentModel.h"
@interface ZZInFactoryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *mutaArr;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic, strong) CcNoDataView *noDataView;
@property (nonatomic,strong)NSString *currentDayStr;
@end

@implementation ZZInFactoryViewController
-(NSMutableArray *)mutaArr
{
    if (!_mutaArr) {
        _mutaArr = [NSMutableArray array];
    }
    return _mutaArr;
}
-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
    
}
-(CcNoDataView *)noDataView
{
    if (_noDataView == nil) {
        _noDataView = [[CcNoDataView alloc] init];
        _noDataView.backgroundColor=[UIColor whiteColor];
    }
    return _noDataView;
    
}

- (void)viewDidLoad {
    
    if ([self.type isEqualToString:@"1"]) {
         self.title = @"预约车辆";
    }else{
          self.title = @"在场车辆";
    }

    [super viewDidLoad];
    [self getCurrentDate];
    [self createTableView];
    [self refreshAction];
    [self.tableView.mj_header beginRefreshing];
    self.startTime=@"";
    self.endTime=@"";
    
}

// 创建tableView
-(void)createTableView
{
    
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, newwidth, newheight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.separatorStyle=0;
        [self.view addSubview:_tableView];
    }
}


-(void)initDataWithDateTime:(NSString *)dateTime andPageIndex:(NSString *)pageIndex
{
    NSString *PageIndex = [NSString stringWithFormat:@"%@",pageIndex];
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];
    if (dateTime.length>0) {
        self.startTime = [NSString stringWithFormat:@"%@00时00分00秒",dateTime];
        self.endTime = [NSString stringWithFormat:@"%@23时59分59秒",dateTime];
    }else{
        self.startTime = @"";
        self.endTime= @"";
    }
    [[ZZInFactoryViewModel shareViewModel]getCarInfoWithCompanyID:loginDict[@"CompanyID"] andPageIndex:PageIndex andType:self.type andStartTime:self.startTime andEndTime:self.endTime andSuccess:^(NSDictionary *result) {
            [self.tableView.mj_header endRefreshing];
        if (result) {
            
            [ZZInFactoryModel mj_setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"rows" : @"ZZInFactoryContentModel",
                         };
            }];
            ZZInFactoryModel *fg = [ZZInFactoryModel mj_objectWithKeyValues:result[@"resultJson"]];
            
            self.total = [fg.total integerValue];
            
            if (!ceilf([fg.total floatValue] / 10.00)) {
                NSInteger x = 0;
                self.num = x;
            }else{
                NSInteger x = ceilf([fg.total floatValue] / 10.00);
                self.num = x;
            }
            
            NSMutableArray *mutableArr=[NSMutableArray array];
            for (NSDictionary *dic in fg.rows) {
                [mutableArr addObject:dic];
            }
            
            
            if (self.index!=1) {
                if (self.index > self.num) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    return ;
                }else{
                    [self.dataArr addObjectsFromArray: mutableArr];
                }
                [self.tableView.mj_footer endRefreshing];
            }
            
            else
            {
                self.dataArr=mutableArr;
                [self.tableView.mj_header endRefreshing];
                
            }
            
               [self.noDataView dataIsEmpty:self.dataArr withView:self.tableView andNoDataView:self.noDataView andRect:CGRectMake(0, 0, newwidth, newheight) andBtnHiddne:YES];
            //[self.noDataView dataIsEmpty:self.dataArr withView:self.tableView andNoDataView:self.noDataView andRect:CGRectMake(0, 130, newwidth, newheight-130) andBtnHiddne:YES];
            self.noDataView.label.text=@"暂无数据";
            
            [self.tableView reloadData];
            
            
        }
        

    } andFailer:^{
        [self.tableView.mj_header endRefreshing];
                [self.noDataView noNetwithView:self.tableView andNoDataView:self.noDataView andRect:CGRectMake(0, 0, newwidth, newheight) andBtnHiddne:NO];
                //点击重新加载的block
                WEAKSELF
                self.noDataView.myBlock=^{
                    [weakSelf.tableView.mj_header beginRefreshing];
                };
                

    }];
                                                                                                                                                                }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZZInFactoryCell *Cell = [ZZInFactoryCell cellWithTableVIew:tableView];
   Cell.model = _dataArr[indexPath.row];
    Cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return Cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}


-(void)getCurrentDate
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *nowdate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit;
    comps = [calendar components:unitFlags fromDate:nowdate];
    
    NSLog(@"%ld年",(long)[comps year]);
    NSLog(@"%ld月",(long)[comps month]);
    NSLog(@"%ld日",(long)[comps day]);
    NSLog(@"%ld时",(long)[comps hour]);
    NSLog(@"%ld分",(long)[comps minute]);
    NSLog(@"%ld秒",(long)[comps second]);
    
    self.currentDayStr= [NSString stringWithFormat:@"%ld年%ld月%ld日",(long)[comps year],(long)[comps month],(long)[comps day]];
}

- (void)refreshAction {
    // 下拉刷新
    if ([self.timeType isEqualToString:@"2"]) {
        weakSelf(weakSelf);
        self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [_dataArr removeAllObjects];
            weakSelf.index = 1;
            
            //[_tableView reloadData];
            //        [weakSelf initDataWithDateTime:weakSelf.currentDayStr andPageIndex:weakSelf.index];
            [weakSelf initDataWithDateTime:@"" andPageIndex:[NSString stringWithFormat:@"%ld",weakSelf.index]];
            
        }];
        
        // 上拉刷新
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            weakSelf.index = weakSelf.index + 1;
            //        [weakSelf initDataWithDateTime:weakSelf.currentMonthStr andPageIndex:weakSelf.index];
            [weakSelf initDataWithDateTime:@"" andPageIndex:[NSString stringWithFormat:@"%ld",weakSelf.index]];
        }];

    }else{
        weakSelf(weakSelf);
        self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [_dataArr removeAllObjects];
            weakSelf.index = 1;
            
            //[_tableView reloadData];
            //        [weakSelf initDataWithDateTime:weakSelf.currentDayStr andPageIndex:weakSelf.index];
            [weakSelf initDataWithDateTime:weakSelf.currentDayStr andPageIndex:[NSString stringWithFormat:@"%ld",weakSelf.index]];
            
        }];
        
        // 上拉刷新
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            weakSelf.index = weakSelf.index + 1;
            //        [weakSelf initDataWithDateTime:weakSelf.currentMonthStr andPageIndex:weakSelf.index];
            [weakSelf initDataWithDateTime:weakSelf.currentDayStr andPageIndex:[NSString stringWithFormat:@"%ld",weakSelf.index]];
        }];

    }
    
   }



@end
